package com.mcms.service.dw;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import com.mcms.util.CalendarUtil;
import com.mcms.util.DBHelper;
import com.mcms.util.GetParamsUtil;
import com.mcms.util.McmsApiConfig;

/**
 * 将客户基本信息插入数据库
 * 
 * @since 2012-7-14
 * @see
 * version 1.0.0 2012-7-14
 */
public class AddCustomInquiryInfoService {

	public static boolean execute(HttpServletRequest request,String uuid){
		
		boolean isSuc=true;
		
		//先通过customId查询该客户是否存在
		boolean isAdd = queryByCustomId(uuid);
		
		//用户不存在执行insert操作，否则执行update操作
		if(isAdd){
			isSuc = updateCustomInquiryInfo(request,uuid);
		} else {
			isSuc = addCustomInquiryInfo(request,uuid);
		}
		
		return isSuc;
	}
	
	/**
	 * 通过customId查询该客户是否存在，如果存在返回true，否则返回false
	 * 
	 * @param customId
	 * @return
	 */
	public static boolean queryByCustomId(String customId){
		boolean isAdd = true;
		String sqlStr = " select * from mcms_custom_inquiry_info t where t.custom_subscribe_id = '"+customId+"'";
		
		try {
			DBHelper dbh = DBHelper.getInstance();
			List list = dbh.query(sqlStr);
			if(list==null || list.size()==0){
				isAdd = false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			isAdd = false;
		}
		return isAdd;
	}
	
	public static boolean addCustomInquiryInfo(HttpServletRequest request,String uuid){
		
		boolean isSuc=true;
		request.setAttribute("message", "新增客户信息成功");
		
		String id = UUID.randomUUID().toString();
		
		//获取参数
		List paramsList = new ArrayList();
		paramsList.add(id);//id
		paramsList.add(uuid);//custom_id
		
		paramsList = GetParamsUtil.getParams(request, "params_addCustom_dw_inquiry",paramsList);
		//
		//参数入库
		String sqlStr = McmsApiConfig.get("sql_addCustom_dw_inquiry");
		try {
			DBHelper dbh = DBHelper.getInstance();
			int i = dbh.execute(sqlStr, paramsList);
			if(i<1){
				isSuc = false;
				request.setAttribute("message", "新增客户问诊信息失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			isSuc = false;
			request.setAttribute("message", "新增客户问诊信息失败");
		}
		
		//返回结果
		return isSuc;
	}
	
	public static boolean updateCustomInquiryInfo(HttpServletRequest request,String uuid){
		boolean isSuc=true;
		request.setAttribute("message", "更新客户信息成功");
		String id = UUID.randomUUID().toString();
		
		//获取参数
		List paramsList = new ArrayList();
//		paramsList.add(id);		//update的时候不需要此参数
		
		paramsList = GetParamsUtil.getParams(request, "params_addCustom_dw_inquiry",paramsList);
		
		paramsList.add(uuid);//客户编号customId要最后放
		
		//参数入库
		String sqlStr = McmsApiConfig.get("sql_updateCustom_dw_inquiry");
		try {
			DBHelper dbh = DBHelper.getInstance();
			int i = dbh.execute(sqlStr, paramsList);
			if(i < 0){
				isSuc = false;
				request.setAttribute("message", "更新客户问诊信息失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			isSuc = false;
			request.setAttribute("message", "更新客户问诊信息失败");
		}
		
		//返回结果
		return isSuc;
	}
}
